<template>
  <div class="home-page">
    <div class="hero-section">
      <div class="hero-content">
        <h1>欢迎来到电商平台</h1>
        <p>发现、分享、交流你喜爱的电商</p>
        <div class="hero-buttons">
          <router-link to="/movies" class="explore-btn">
            浏览电商
          </router-link>
          <router-link 
            v-if="!userStore.isLoggedIn" 
            to="/register" 
            class="register-btn"
          >
            立即注册
          </router-link>
        </div>
      </div>
    </div>
    
    <div class="featured-section">
      <h2>精选推荐</h2>
      <div class="movie-grid">
        <MovieCard 
          v-for="movie in featuredMovies" 
          :key="movie.id" 
          :movie="movie"
          @click="viewDetail(movie.id)"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { movieApi } from '../utils/api'
import MovieCard from '../components/MovieCard.vue'

export default {
  components: { MovieCard },
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const featuredMovies = ref([])
    
    const viewDetail = (id) => {
      router.push({ name: 'MovieDetail', params: { id } })
    }
    
    onMounted(async () => {
      try {
        const response = await movieApi.getList()
        featuredMovies.value = response.data.slice(0, 4) // 取前4部作为推荐
      } catch (error) {
        console.error('获取推荐电商失败:', error)
      }
    })
    
    return {
      userStore,
      featuredMovies,
      viewDetail
    }
  }
}
</script>

<style scoped>
.home-page {
  max-width: 1200px;
  margin: 0 auto;
}

.hero-section {
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), 
              url('https://via.placeholder.com/1200x600?text=Movie+Background');
  background-size: cover;
  background-position: center;
  height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  margin-bottom: 50px;
  border-radius: 8px;
  overflow: hidden;
}

.hero-content {
  max-width: 800px;
  padding: 20px;
}

.hero-content h1 {
  font-size: 3rem;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.hero-content p {
  font-size: 1.5rem;
  margin-bottom: 30px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.hero-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.explore-btn, .register-btn {
  padding: 12px 30px;
  border-radius: 30px;
  text-decoration: none;
  font-weight: bold;
  transition: all 0.3s;
}

.explore-btn {
  background-color: var(--primary-color);
  color: white;
  border: 2px solid var(--primary-color);
}

.explore-btn:hover {
  background-color: transparent;
  color: var(--primary-color);
}

.register-btn {
  background-color: transparent;
  color: white;
  border: 2px solid white;
}

.register-btn:hover {
  background-color: white;
  color: var(--dark-bg);
}

.featured-section {
  padding: 0 20px 50px;
}

.featured-section h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 2rem;
  color: var(--dark-bg);
}

.movie-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 30px;
}

@media (max-width: 768px) {
  .hero-content h1 {
    font-size: 2rem;
  }
  
  .hero-content p {
    font-size: 1.2rem;
  }
  
  .hero-buttons {
    flex-direction: column;
    align-items: center;
  }
  
  .movie-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}
</style>